﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using SoftwareBase.Handler;
using SoftwareBase.Verify;

namespace SoftwareBase.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PostController : ControllerBase
    {
        [HttpPost("check")]
        public ActionResult Check([FromForm][Required] long sno, [FromForm] [Required] string password)
        {
            if (VerifyManager.CheckUser(sno, password))
            {
                return Ok("检查成功！");
            }

            return Unauthorized("很抱歉，您不属于我们基地！无法提交作品！");
        }

        [DisableRequestSizeLimit]
        [HttpPost("d")]
        public async Task<ActionResult> Python([FromForm] [Required] PostBody body)
        {
            return await PostActionHandler(body, Direction.DataAnalyze);
        }

        [DisableRequestSizeLimit]
        [HttpPost("f")]
        public async Task<ActionResult> FrontEnd([FromForm][Required] PostBody body)
        {
            return await PostActionHandler(body, Direction.FrontEnd);
        }

        [DisableRequestSizeLimit]
        [HttpPost("b")]
        public async Task<ActionResult> BackEnd([FromForm][Required] PostBody body)
        {
            return await PostActionHandler(body, Direction.BackEnd);
        }

        public async Task<ActionResult> PostActionHandler(PostBody body, Direction direction)
        {
            var (sno, password, name, video, code) = body;
            if (!VerifyManager.CheckUser(sno, password)) return Unauthorized("很抱歉，您的账户验证失败，提交系统不允许您提交作品！也可能是提交系统并不包含您的信息！");
            if (!VerifyManager.CheckFile(video.FileName, code.FileName)) return BadRequest("文件格式非法！视频必须为mp4,压缩包必须为zip！");
            if (!VerifyManager.CheckChance(sno)) return BadRequest("很抱歉，您已提交过三次作品，无法继续提交！");
            var work = new Work
            {
                Sno = sno,
                Direction = direction,
                Name = name,
                Code = code,
                Video = video
            };
            try
            {
                await work.SaveAsync();
                Log(sno, name, direction);
            }
            catch (Exception)
            {
                return BadRequest("文件异常，请重新打包zip！");
            }
            return Ok("提交成功");
        }

        private void Log(long sno, string name, Direction direction)
        {
            var time = DateTime.Now.ToString("MM-dd HH:mm:ss");
            Console.WriteLine($"[{time}] [{direction}]=>[{sno}]-[{name}]: Post Success ");
        }
    }

    public class PostBody
    {
        [Required]
        public long Sno { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public IFormFile Video { get; set; }
        [Required]
        public IFormFile Code { get; set; }

        public void Deconstruct(out long sno, out string password, out string name, out IFormFile video, out IFormFile code)
        {
            sno = Sno;
            password = Password;
            name = Name;
            video = Video;
            code = Code;
        }

    }

    
}